//
//  GKStatusPictureView.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/14.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit
import SDWebImage
//计算图片宽高
    private let itemWH = ( screenW - 2 * GKStatusCellMargin - (CGFloat(GKMBPicMaxColum) - 1) * GKStatusCellMargin)/CGFloat(GKMBPicMaxColum)

//重用cellid
    private let cellID = "pictureCollectionViewCell"

class GKStatusPictureView: UICollectionView {

    
    
    private var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    var picURLs:[NSURL]?
    {
        
        didSet
        {
            let size = calSize()
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            //TODO
//            GKPrint("计算的size\(size)")
            
            //cell 的重用 要更新数据
            reloadData()
        }
        
    }
    
    private func calSize() -> CGSize {
        
        //单张图片或没有图片的情况
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        let count  = picURLs?.count ?? 0
        
        if count == 0
        {
//            GKPrint("没有图片")
            return CGSizeZero
        }
        if count == 1
        {
            //TODO : 返回图片本来大小
//            let size = CGSize(width: 180, height: 180)
//            layout.itemSize = size
//            return size
            
            var size = CGSize(width: itemWH, height: itemWH)
            let url = picURLs?.first
            
            //获取缓存图片
            if let img = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(url?.absoluteString)
            {
                size = img.size
                size = CGSize(width: size.width * UIScreen.mainScreen().scale, height: size.height * UIScreen.mainScreen().scale)
            }
            //假如图片太大太小都处理一下
            if size.width < 40
            {
                size.width = 40
            }
            if size.width > screenW * 0.5
            {
                size.width = screenW * 0.5
            }
            layout.itemSize = size
            return size
        }
        
        //多张图片的情况
        layout.minimumInteritemSpacing = GKStatusCellMargin
        layout.minimumLineSpacing = GKStatusCellMargin
        
        var colum = GKMBPicMaxColum
        
        if count == 2 || count == 4
        {
            colum = 2//只有当图片为两张或四张的时候是两列的情况
        }
        
        // 服务器开发用得比较多,分页,保证列数是整数
        // 行数 = (总个数 + 列数 - 1) / 列数
        let row = ( count + colum - 1) / colum
        // 计算宽度和高度
        // 宽度 = 列数 * item宽度 + (列数 - 1) * 列间距
        let width = CGFloat(colum) * itemWH + (CGFloat(colum) - 1) * GKStatusCellMargin + 2
        // 高度度 = 行数 * item高度 + (行数 - 1) * 行间距
        let height = CGFloat(row) * itemWH + ( CGFloat(row) - 1) * GKStatusCellMargin + 2
        
        let size = CGSize(width: width , height: height)
        return size
    }
    
    
    
    init(){
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        self.backgroundColor = UIColor.clearColor() 
        self.dataSource = self
        //注册cell
        self.registerClass(GKStatusPictureViewCell.self, forCellWithReuseIdentifier: cellID)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}




//数据源代理方法实现
extension GKStatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        GKPrint("图片的张数-------------\(picURLs?.count)")
        return picURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! GKStatusPictureViewCell
        cell.imgURL = picURLs![indexPath.item]
        
        return cell
    }
    
}




//collectionviewcell 自定义
class GKStatusPictureViewCell: UICollectionViewCell
{
    var imgURL: NSURL?{
        didSet{
            bgImgView.sd_setImageWithURL(imgURL!)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI(){
        
        contentView.addSubview(bgImgView)
        bgImgView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    //懒加载
    //图片view
    private lazy var bgImgView: UIImageView = UIImageView()
}
